package cn.sunline.rest.config;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sunline.rest.annotation.Controller;
import cn.sunline.rest.annotation.Req;
import cn.sunline.rest.annotation.Req.All;
import cn.sunline.rest.annotation.Req.Delete;
import cn.sunline.rest.annotation.Req.Get;
import cn.sunline.rest.annotation.Req.Post;
import cn.sunline.rest.annotation.Req.Update;
import cn.sunline.rest.util.ClassTools;

/**
 * @ClassName RestConfig
 * @Description 项目主要配置位置  本类为单例
 * @author zhangli
 * @Date 2017年2月7日 下午2:47:48
 * @version 1.0.0
 */
public class RestConfig {
    private static String POST_FLAG = "post";
    private static String GET_FLAG = "get";
    private static String DELETE_FLAG = "delete";
    private static String UPDATE_FLAG = "update";
    private static String All_FLAG = "all";
    
    private Map<String,Method> map = new HashMap<>();

    private static final RestConfig me = new RestConfig();
    
    private RestConfig() {
    }
    
    public static RestConfig me(){
        return me;
    }
    
    /**
     * @Description 添加Controller
     * @param pack
     */
    public void addCtrPack(String pack){
        List<Class<?>> ctrs = ClassTools.scanClassWithAnno(pack, Controller.class);
        for (Class<?> ctr : ctrs) {
            Method[] methods = ctr.getMethods();
            for (Method method : methods) {
                Annotation[] annotations = method.getAnnotations();  
                for (Annotation annotation : annotations) {  
                    // 获取注解的具体类型  
                    Class<? extends Annotation> annotationType = annotation.annotationType();  
                    //get 请求
                    if (Req.Get.class == annotationType) {
                        Req.Get get = (Get) annotation;
                        String path = get.value();
                        map.put(path + GET_FLAG, method);
                    }
                    //post 请求
                    if (Req.Post.class == annotationType) { 
                        Req.Post post = (Post) annotation;
                        String path = post.value();
                        map.put(path + POST_FLAG, method);
                    }
                    //delete 请求
                    if (Req.Delete.class == annotationType) {
                        Req.Delete delete = (Delete) annotation;
                        String path = delete.value();
                        map.put(path + DELETE_FLAG, method);
                    }
                    // update 请求
                    if (Req.Update.class == annotationType) {
                        Req.Update update = (Update) annotation;
                        String path = update.value();
                        map.put(path + UPDATE_FLAG, method);
                    }
                    //All 请求
                    if (Req.All.class == annotationType) {
                        Req.All all = (All) annotation;
                        String path = all.value();
                        map.put(path + All_FLAG, method);
                    }
                }   
            }
        }
    }
}
